package com.card.manager.services.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import com.card.framework.mybatis.mapper.MyFrameworkSupperMapper;
import com.card.framework.page.model.PageModel;
import com.card.manager.dao.MafMapper;
import com.card.manager.domain.Maf;
import com.card.manager.domain.MafExample;
import com.card.manager.domain.MafExample.Criteria;
import com.card.manager.services.MafService;

@Service("MafServiceImpl")
public class MafServiceImpl extends MyFrameworkSupperMapper implements MafService{
	@Override
	public Map<String, Object> selectMafForPage(Maf maf, PageModel pageModel) {
		MafMapper mapper = this.getMapper(MafMapper.class);
		MafExample mafExample = new MafExample();
		Criteria oredCriteria  = mafExample.createCriteria();
		mafExample.setPage(pageModel);
		if(StringUtils.isNotEmpty(pageModel.getSort())) 
		mafExample.setOrderByClause(pageModel.getSort()+" "+pageModel.getOrder());
		if(StringUtils.isNotEmpty(maf.getCreatuserid())) 
			oredCriteria.andCreatuseridEqualTo(maf.getCreatuserid());
		if(StringUtils.isNotEmpty(maf.getItemid())) 
			oredCriteria.andItemidEqualTo(maf.getItemid());
		if(StringUtils.isNotEmpty(maf.getCardid())) 
			oredCriteria.andCardidEqualTo(maf.getCardid());
		
		if(StringUtils.isNotEmpty(maf.getBegincreatdatetime())){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date;
			try {
				date = sdf.parse(maf.getBegincreatdatetime());
				oredCriteria.andCreatdatetimeGreaterThanOrEqualTo(date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		if(StringUtils.isNotEmpty(maf.getEndcreatdatetime())){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date;
			try {
				date = sdf.parse(maf.getEndcreatdatetime());
				oredCriteria.andCreatdatetimeLessThanOrEqualTo(date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		
		List<Maf> list = mapper.selectByExample(mafExample);
		Map<String, Object> modelMap = new HashMap<String, Object>();  
	    modelMap.put("total", pageModel.getTotalCount());  
	    modelMap.put("rows", list); 
	    return modelMap;
	}
	
	@Override
	public List<Maf> selectMafForPrint(Maf maf) {
		MafMapper mapper = this.getMapper(MafMapper.class);
		MafExample mafExample = new MafExample();
		Criteria oredCriteria  = mafExample.createCriteria();
		if(StringUtils.isNotEmpty(maf.getCreatuserid())) 
			oredCriteria.andCreatuseridEqualTo(maf.getCreatuserid());
		if(StringUtils.isNotEmpty(maf.getItemid())) 
			oredCriteria.andItemidEqualTo(maf.getItemid());
		if(StringUtils.isNotEmpty(maf.getCardid())) 
			oredCriteria.andCardidEqualTo(maf.getCardid());
		
		if(StringUtils.isNotEmpty(maf.getBegincreatdatetime())){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date;
			try {
				date = sdf.parse(maf.getBegincreatdatetime());
				oredCriteria.andCreatdatetimeGreaterThanOrEqualTo(date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		if(StringUtils.isNotEmpty(maf.getEndcreatdatetime())){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date;
			try {
				date = sdf.parse(maf.getEndcreatdatetime());
				oredCriteria.andCreatdatetimeLessThanOrEqualTo(date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		
		List<Maf> list = mapper.selectByExample(mafExample);
	    return list;
	}

}